package cn.iocoder.yudao.module.gateway.controller.admin.deviceconfig;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.gateway.controller.admin.deviceconfig.vo.*;
import cn.iocoder.yudao.module.gateway.dal.dataobject.deviceconfig.DeviceConfigDO;
import cn.iocoder.yudao.module.gateway.service.deviceconfig.DeviceConfigService;

@Tag(name = "管理后台 - 设备配置")
@RestController
@RequestMapping("/gateway/device-config")
@Validated
public class DeviceConfigController {

    @Resource
    private DeviceConfigService deviceConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建设备配置")
    @PreAuthorize("@ss.hasPermission('gateway:device-config:create')")
    public CommonResult<Long> createDeviceConfig(@Valid @RequestBody DeviceConfigSaveReqVO createReqVO) {
        return success(deviceConfigService.createDeviceConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备配置")
    @PreAuthorize("@ss.hasPermission('gateway:device-config:update')")
    public CommonResult<Boolean> updateDeviceConfig(@Valid @RequestBody DeviceConfigSaveReqVO updateReqVO) {
        deviceConfigService.updateDeviceConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('gateway:device-config:delete')")
    public CommonResult<Boolean> deleteDeviceConfig(@RequestParam("id") Long id) {
        deviceConfigService.deleteDeviceConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备配置")
                @PreAuthorize("@ss.hasPermission('gateway:device-config:delete')")
    public CommonResult<Boolean> deleteDeviceConfigList(@RequestParam("ids") List<Long> ids) {
        deviceConfigService.deleteDeviceConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('gateway:device-config:query')")
    public CommonResult<DeviceConfigRespVO> getDeviceConfig(@RequestParam("id") Long id) {
        DeviceConfigDO deviceConfig = deviceConfigService.getDeviceConfig(id);
        return success(BeanUtils.toBean(deviceConfig, DeviceConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备配置分页")
    @PreAuthorize("@ss.hasPermission('gateway:device-config:query')")
    public CommonResult<PageResult<DeviceConfigRespVO>> getDeviceConfigPage(@Valid DeviceConfigPageReqVO pageReqVO) {
        PageResult<DeviceConfigDO> pageResult = deviceConfigService.getDeviceConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备配置 Excel")
    @PreAuthorize("@ss.hasPermission('gateway:device-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceConfigExcel(@Valid DeviceConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceConfigDO> list = deviceConfigService.getDeviceConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备配置.xls", "数据", DeviceConfigRespVO.class,
                        BeanUtils.toBean(list, DeviceConfigRespVO.class));
    }

}